/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-15 12:47:07 +0000 (Sun, 15 Feb 2009) $:
 * @version $Revision: 364 $:
 */
package dk.hewison.client;


import static dk.hewison.client.AppEvent.Event.PAGE_SELECTED;
import dk.hewison.client.domain.Directory;
import dk.hewison.client.menu.MainMenuController;
import static dk.hewison.client.menu.MainMenuController.SLIDESHOW;
import dk.hewison.client.mvc.AbstractController;
import dk.hewison.client.mvc.Controller;
import dk.hewison.client.mvc.View;
import dk.hewison.client.pages.PageStackController;
import dk.hewison.client.panels.ViewFactory;

public class MediaAppController extends AbstractController<Directory> {

    public static final int MAIN_MENU_ID = 0;
    public static final int PAGE_STACK = 1;


    private static MediaAppController ourInstance = new MediaAppController();
    private int currentPageId = -1;

    public static MediaAppController getInstance() {
        return ourInstance;
    }

    private MediaAppController() {
        registerEvent(PAGE_SELECTED);
    }


    protected Controller[] initSubControllers() {
        MainMenuController mainMenuController = new MainMenuController(MAIN_MENU_ID);
        PageStackController pageStackController = new PageStackController(PAGE_STACK);

        return new Controller[]{mainMenuController, pageStackController};
    }

    protected void updateModel() {

    }

    public View<Directory> createView() {
        return ViewFactory.getInstance().createMediaAppView();
    }

    @Override
    public void eventOccurred(AppEvent event) {
        if (PAGE_SELECTED == event.getEvent()) {
            showPage((Integer) event.getObject());
        }
    }

    private void showPage(int pageId) {
        if (pageId == currentPageId) return;

        Controller subController = getSubController(pageId);
        if (subController != null) {
            boolean b = subController.leavePage();
            if (!b) return;
        }

        getSubView(PAGE_STACK).setModel(pageId);

        ((MediaAppView) getView()).showMenu(pageId != SLIDESHOW);
        currentPageId = pageId;
    }

    public int getCurrentPageId() {
        return currentPageId;
    }

    public Directory getModel() {
        return null;
    }

    public void setModel(Directory data) {

    }
}
